<?php

namespace Addons\IntegralShop\Controller;

use Addons\IntegralShop\Repositor\IsSpreadsRecordRepositor;
use Mp\Controller\MobileBaseController;

use Addons\IntegralShop\Repositor\IsGoodRepositor;
use Addons\IntegralShop\Repositor\IsOrderRepositor;
use Addons\IntegralShop\Repositor\IsUserRepositor;

require_once(ADDON_PATH . 'IntegralShop' . '/functions.php');

/**
 * 积分商城移动端控制器
 * @author 16,sg
 */
class MobileController extends MobileBaseController
{
    public function _initialize()
    {
        parent::_initialize();
    }
    /*** view ***/

    /**
     * 首页
     */
    public function index()
    {
        $this->display();
    }

    /**
     * 积分兑换
     */
    public function exchangeShop()
    {
        $this->display();
    }

    /*** api ***/
    /**
     * 获取用户信息
     */
    public function api_init()
    {
        $userRepo = new IsUserRepositor();
        $recordRepo = new IsSpreadsRecordRepositor();

        // 获取相关配置
        $conf = get_addon_settings();
        $conf || $conf = [];

        // 获取当前用户唯一 key
        $fansInfo = get_fans_info(get_openid());
        $unId = int2Str($fansInfo['id']);

        // 获取推广数量

        return $this->_json(0, 'success', [
            "nickname" => $fansInfo["nickname"],
            "headimgurl" => $fansInfo["headimgurl"],
            "is_subscribe" => $fansInfo["is_subscribe"],
            "mark" => $userRepo->getCurUserMark(),
            "spread_num" => $recordRepo->getSpreadNum(get_openid()),
            'un' => $unId,
            "conf" => $conf
        ]);
    }

    /**
     * 初始化用户信息
     */
    public function api_user_init()
    {
        $unId = I("un", "");

        $userRepo = new IsUserRepositor();

        // 是否添加推广奖励
        // 有推广人且用户第一次访问
        $needAddMark = !empty($unId) && $userRepo->getCurUserMark() == -1;

        if ($userRepo->init()) {
            if ($needAddMark) {
                // 给用户添加推广奖励
                $userRepo->addMark2Un($unId);
            }

            $this->_json(0, 'success');
        } else {
            $this->_json(1, $userRepo->getError());
        }
    }

    /**
     * 商品列表
     */
    public function api_goods()
    {
        $page = I("page", 1, 'intval');
        $size = I("size", 15, 'intval');

        $size > 100 && $size = 100;

        $repo = new IsGoodRepositor();
        $this->_json(0, 'success', ['list' => $repo->lists($page, $size)]);
    }

    public function api_good_detail(){
        $good_id = I("good_id", "0",'intval');

        if($good_id == 0) $this->_json(1,'非法标识');

        $repo = new IsGoodRepositor();
        $info = $repo->detail($good_id);
        if($info === false){
            $this->_json(1,$repo->getError());
        }else{
            $this->_json(0, 'success', ['detail' => $info]);
        }
    }

    /**
     * 提交订单
     */
    public function api_add_order()
    {
        $params['good_id'] = I('post.good_id', 0, 'intval');
        $params['name'] = I('post.name', '');
        $params['address_code'] = I('post.address_code', '');
        $params['address'] = I('post.address', '');
        $params['phone'] = I('post.phone', '');

        if ($params['good_id'] == 0) $this->_json(1, '商品不能为空');
        if ($params['name'] == "") $this->_json(1, '请输入用户名');
        if ($params['address_code'] == "") $this->_json(1, '请输入邮政编码');
        if ($params['address'] == "") $this->_json(1, '请输入收货地址');
        if ($params['phone'] == "") $this->_json(1, '请输入手机号码');

        $repo = new IsOrderRepositor();
        if (!$repo->addOrder($params)) {
            $this->_json(1, $repo->getError());
        } else {
            $this->_json(0, 'success');
        }
    }

    /**
     * 用户订单列表
     */
    public function api_orders()
    {
        $repo = new IsOrderRepositor();
        $this->_json(0, 'success', ['list' => $repo->userOrderlist()]);
    }

    /**
     * 积分记录
     */
    public function api_records()
    {
        $repo = new IsSpreadsRecordRepositor();
        $this->_json(0, 'success', ['list' => $repo->userlist()]);
    }

    private function _json($code, $msg, $data = [])
    {
        $this->ajaxReturn(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }
}
